package ro.one.gaeql.impl;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;

public class LogInstrumentation implements Instrumentation {
	private static final long serialVersionUID = 1L;
	private String idJob;
	
	public LogInstrumentation(String idJob) {
		this.idJob = idJob;
	}
	
	@Override
	public void datastoreQueryBuilt(Query q) { }

	@Override
	public void usingQueryBuilt(Entity usingEntity, Query q) { }

	@Override
	public void settingProperty(Entity entity, String propertyName, Object value) {
		Entity logEntry = new Entity("Log");
		
		logEntry.setProperty("idJob", idJob);
		logEntry.setProperty("entity", entity.getKind());
		logEntry.setProperty("propertyName", propertyName);
		logEntry.setProperty("value", value);
		logEntry.setProperty("oldValue", entity.getProperty(propertyName));
		
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		ds.put(logEntry);
	}
}
